package com.gitee.minimalismstyle.fresh.common.document.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.Global
import com.gitee.minimalismstyle.fresh.common.document.controller.ExeclController
import com.gitee.minimalismstyle.fresh.common.document.domain.Workbook
import com.gitee.minimalismstyle.fresh.common.document.service.WorkbookService
import org.springframework.stereotype.Service
import java.io.FileReader
import javax.xml.bind.JAXBContext
import javax.xml.bind.Unmarshaller

@Service
class XmlWorkbookServiceImpl : WorkbookService {

    companion object{
        val xmlMapping: MutableMap<String, Workbook> = hashMapOf()
    }

    override fun findById(id: String): Workbook{
        var workbook : Workbook;
        if(!Global.isDebug && xmlMapping.containsKey(id)){
            workbook = xmlMapping[id]!!
        }else{
            val context: JAXBContext = JAXBContext.newInstance(Workbook::class.java)
            val unmarshaller: Unmarshaller = context.createUnmarshaller()
            val xmlPath = ExeclController::class.java.getResource("/import/${id}.xml").path
            val fr = FileReader(xmlPath)
            workbook = unmarshaller.unmarshal(fr) as Workbook;
        }
        return workbook
    }
}